source ~/.local/bin/ubuild.sh
NPM="npm -registry=https://registry.npm.taobao.org"
cd $SCRIPT_FOLDER


install_bootstrap(){
  <<EOF
  cnpm install jquery --save
  npm audit fix
  cnpm i --save popper.js
EOF
  cnpm uninstall bootstrap
  cnpm install bootstrap@3.3.7 --save

  f=build/webpack.base.conf.js
  if ! grep --quiet 'jQuery' $f;then
    file=`basename -- $f`
    cp -f $f log/$file-`date +"%F-%T"`
    sed -f - -i -E $f<<EOF
1 i\
const webpack = require('webpack')\n
EOF
    sed -i --regexp-extended '/.*module.exports =.*/r /dev/stdin' $f<<EOF
plugins: [ new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "windows.jQuery": "jquery" }) ],
EOF
  fi

  f=src/main.js
  if ! grep --quiet 'jquery' $f;then
    file=`basename -- $f`
    cp -f $f log/$file-`date +"%F-%T"`
    sed -f - -i -E src/main.js<<EOF
1 i\
import $ from 'jquery'\n\
import 'bootstrap/dist/css/bootstrap.min.css'\n\
import 'bootstrap/dist/js/bootstrap.min'\n
EOF
  fi

}

install(){
  <<EOF
  npm i @smallwei/avue -S -registry=https://registry.npm.taobao.org
  $NPM install axios --save
  $NPM install vuex --save
  $NPM install element-ui --save
  $NPM install webpack --save-dev
  $NPM install sass-loader --save
  $NPM install node-sass --save
$NPM install
vue add vuetify
$NPM install @mdi/font -D
$NPM install @mdi/js -D
  $NPM install --save @/plugins/vuetify
  $NPM install vuetify
  $NPM install sass sass-loader@7.0.3 deepmerge -D
EOF
  cnpm install vue-i18n
}
remove(){
  npm remove sass-loader
  npm remove node-sass
  npm remove webpack
}

dev(){
  npm run dev
}

restart(){
  shutdown
  sleep 2
  pack
  daemon
  log
}

daemon(){
  shutdown
  nohup npm run dev 1>$SCRIPT_FOLDER/log/app.log 2>&1 &
  echo $!>$SCRIPT_FOLDER/pid
}

log(){
  tail -f $SCRIPT_FOLDER/log/app.log
}

pack(){
  npm run build
}

shutdown(){
  if [ -f pid ];then
    pid=`cat pid`
    if test -z "$pid"; then :; else sudo kill 9 $pid; fi
  fi
  sudo pkill $ROOT
  ps -ef|grep $ROOT|awk '{print $2}'|xargs sudo kill -9 {}
  ps -ef|grep $ROOT
}

status(){
  ps -ef|grep $ROOT
}

$@
cd $CD
